package com.example.multithread.no1114;

import java.util.concurrent.atomic.AtomicInteger;

public class AtomicFoo {

    public AtomicFoo() {

    }

    AtomicInteger two = new AtomicInteger(0);
    AtomicInteger three = new AtomicInteger(0);

    public static void main(String[] args) {
        AtomicFoo foo = new AtomicFoo();

        new Thread(() -> {
            foo.first();
        }).start();
        new Thread(() -> {
            foo.second();
        }).start();
        new Thread(() -> {
            foo.third();
        }).start();
    }

    public void first() {

        System.out.print("first");
        two.getAndIncrement();
    }

    public void second() {
        while (two.get() != 1) {}
        System.out.print("two");
        three.getAndIncrement();
    }

    public void third() {
        while (three.get() != 1) {}
        System.out.print("three");
    }
}
